package com.cennavi.minemap.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import sun.misc.BASE64Encoder;

@SuppressWarnings("restriction")
public class StringUtil {
	public static String reverse(String input) {
        StringBuilder builder = (new StringBuilder(input)).reverse();
        return builder.toString();
    }

    public static String pretreatment(String input) {
        return null == input?"":("".equals(input.trim())?"":("NULL".equals(input.trim().toUpperCase())?"":input.trim()));
    }

    public static List<String> split(String input, String regex) {
        List<String> list = new ArrayList<String>();
        if(null == input) {
            return list;
        } else if(input.trim().length() == 0) {
            list.add("");
            return list;
        } else {
            String[] array = input.trim().split(regex);
            String[] arr$ = array;
            int len$ = array.length;

            for(int i$ = 0; i$ < len$; ++i$) {
                String str = arr$[i$];
                list.add(str);
            }

            return list;
        }
    }

    public static String fillLeft(String content, String c, long length) {
        while((long)content.length() < length) {
            content = c + content;
        }

        return content;
    }

    public static String fillRight(String content, String c, long length) {
        while((long)content.length() < length) {
            content = content + c;
        }

        return content;
    }

    public static String leftTrimByStr(String input, String c) {
        return input.startsWith(c)?leftTrimByStr(input.substring(1, input.length()), c):input;
    }

    public static String rightTrimByStr(String input, String c) {
        return input.endsWith(c)?rightTrimByStr(input.substring(0, input.length() - 1), c):input;
    }

    public static String bothTrimByStr(String input, String c) {
        String trimString = leftTrimByStr(input, c);
        trimString = rightTrimByStr(trimString, c);
        return trimString;
    }

    public static String getRandomString(int length) {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();

        for(int i = 0; i < length; ++i) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }

        return sb.toString();
    }

    public static boolean isEmpty(String str) {
        return str == null || str.isEmpty();
    }

    public static boolean equals(String str1, String str2) {
        return str1 == null?str2 == null:str1.equals(str2);
    }
    @SuppressWarnings("rawtypes")
    public static <T> String join(Iterable<T> elements, String delim) {
        StringBuilder result = new StringBuilder();
        boolean first = true;

        Object elem;
        for(Iterator i$ = elements.iterator(); i$.hasNext(); result.append(elem)) {
            elem = i$.next();
            if(first) {
                first = false;
            } else {
                result.append(delim);
            }
        }

        return result.toString();
    }

    public static String uuid() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString();
    }

    public static String lessUUID() {
        UUID uuid = UUID.randomUUID();
        long most = uuid.getMostSignificantBits();
        long least = uuid.getLeastSignificantBits();
        return digits(most >> 32, 8) + digits(most >> 16, 4) + digits(most, 4) + digits(least >> 48, 4) + digits(least, 12);
    }

    private static String digits(long val, int digits) {
        long hi = 1L << digits * 4;
        return Long.toHexString(hi | val & hi - 1L).substring(1);
    }

    public static String encoderByMd5(String str) {
        MessageDigest md5 = null;

        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException var6) {
            var6.printStackTrace();
        }

        BASE64Encoder base64en = new BASE64Encoder();
        String newstr = null;

        try {
            newstr = base64en.encode(md5.digest(str.getBytes("utf-8")));
        } catch (UnsupportedEncodingException var5) {
            var5.printStackTrace();
        }

        return newstr;
    }
    
	public static String createKey(String dataSource, int x, int y, int z){
		String key = dataSource;		
		for(int i=0; i<(20-dataSource.length()); i++){
			key = "0" + key;
		}		
		key += String.format("%08d", x);
		
		key += String.format("%08d", y);
		
		key += String.format("%02d", z);
		
		return key;
	}
	
	public static String getRandNum(int min, int max) {
	    int randNum = min + (int)(Math.random() * ((max - min) + 1));
	    return String.valueOf(randNum);
	}
	
    public static void main(String[] args){
//    	System.out.println(StringUtil.encoderByMd5("admin"));
//    	System.out.println("随机数为" + getRandNum(100000,999999));
//    	System.out.println(StringUtil.uuid());
    	System.out.println(StringUtil.encoderByMd5("minelab"));
    }
}
